<template>
    <div class="login_container">
    <div class="login_box">
        <div class="login_a"> <img src="../assets/logo.png"></div>
        <div class="login_form">
        <el-form ref="loginref" :model="loginform" :rules="loginrules" label-width="0px">
  <el-form-item prop="username" >
    <el-input v-model="loginform.username" prefix-icon="el-icon-user" ></el-input>
  </el-form-item>
  <el-form-item prop="password">
    <el-input v-model="loginform.password" prefix-icon="el-icon-lock" type="password"></el-input>
  </el-form-item>
        </el-form>
        <div class="btns"><el-button type="primary" @click="login">登录</el-button>
        <el-button type="info" @click="resetloginref">重置</el-button></div>
    </div>
    </div>
    </div>
</template>
<script>
export default {
    data() {
        return {
            loginform: {
                username:'admin',
                password:'123456'
            },
            loginrules: {
               username: [
                   { required: true, message: '请输入账号名称', trigger: 'blur' },
                   { min: 2, max: 5, message: '长度在 2 到 5 个字符', trigger: 'blur' }
               ],
               password: [
                   { required: true, message: '请输入密码', trigger: 'blur' },
                   { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
               ]
            }
        }
    },
    methods: {
        resetloginref() {
        this.$refs.loginref.resetFields();
        },
        login() {
        this.$refs.loginref.validate( async valid => {
            if(!valid) return;
            const { data : res} = await this.$http.post("login", this.loginform);
            if(res.meta.status !==200)return this.$mes.error("登录失败！");
            this.$mes.success("登录成功");
            window.sessionStorage.setItem("token", res.data.token);
            this.$router.push("/home")
        })
        }
    },
}
</script>
<style lang="less" scoped>
.login_container{
    background-color: #2b4b6b;
    height: 100%;
}
.login_box{
    width: 450px;
    height: 300px;
    background-color: #fff;
    border-radius: 5px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%,-50%);
    .login_a{
        width: 130px;
        height: 130px;
        border: 3px solid #eee;
        border-radius: 50%;
        padding: 10px;
        position: absolute;
        left: 50%;
        transform: translate(-50%,-50%);
        background-color: #fff;
        img{
            width: 100%;
            height: 100%;
            border-radius: 50%;
            background-color: #eee;
        }
    }
}
.login_form{
position: absolute;
bottom: 0;
left: 50%;
transform: translate(-50%,-20%);
}
.btns{
    display: flex;
    justify-content: flex-end;
}
</style>